package com.attendance.controller;

import com.attendance.pojo.Role;
import com.attendance.result.LayuiMiniPage;
import com.attendance.service.MenuServiceImpl;
import com.attendance.service.RoleServiceImpl;
import com.attendance.web.Result;
import com.attendance.web.ResultCode;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.google.common.base.Joiner;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.security.RolesAllowed;
import java.util.ArrayList;
import java.util.List;

/**
 * 角色控制器
 *
 * @author 今朝
 * @date 2021/03/17
 */
@Controller
@RequestMapping("/system/role")
@RolesAllowed("ADMIN")
public class RoleController {

    @Autowired
    RoleServiceImpl roleService;
    @Autowired
    MenuServiceImpl menuService;

    @GetMapping()
    public String table() {
        return "/system/role/table";
    }

    @GetMapping("/add")
    public String add() {
        return "/system/role/add";
    }

    @GetMapping("/binding/{ids}")
    public ModelAndView binding(@PathVariable String ids) {
        ModelAndView modelAndView = new ModelAndView("/system/role/binding");
        modelAndView.addObject("uIds", ids);
        return modelAndView;
    }

    @GetMapping("/edit/{id}")
    public ModelAndView editForId(@PathVariable Long id) {
        ModelAndView modelAndView = new ModelAndView("/system/role/edit");
        Role role = roleService.selectById(id);
        List<Long> menuIds = roleService.getMenuIdsById(role.getId());
        modelAndView.addObject("role", role);
        modelAndView.addObject("menuIds", Joiner.on(",").join(menuIds));
        modelAndView.addObject("menuNames", Joiner.on(",").join(menuService.getMenusTitleByIds(menuIds)));
        return modelAndView;
    }

    @GetMapping("/edit")
    public String edit() {
        return "/system/role/edit";
    }

    @ResponseBody
    @GetMapping("/listPage")
    public LayuiMiniPage<Role> getListPage(Integer page, Integer limit, String searchParams) {
        if (searchParams != null) {
            Page<Role> listPage = roleService.getListPage(page, limit, searchParams);
            return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
        }
        Page<Role> listPage = roleService.getListPage(page, limit);
        return new LayuiMiniPage<>(200, "查询成功", listPage.getTotal(), listPage.getRecords());
    }


    @ResponseBody
    @PutMapping("/add")
    public Result add(Role role, String menuIds) {
        if (roleService.add(role, menuIds)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PostMapping("/update")
    public Result update(Role role, String menuIds) {
        if (roleService.updateById(role, menuIds)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @PostMapping("/binding")
    public Result binding(String uIds, String rIds) {
        if (roleService.binding(uIds, rIds)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }

    @ResponseBody
    @DeleteMapping("/delete")
    public Result delete(@RequestParam("ids[]") ArrayList<Integer> ids) {
        if (roleService.delete(ids)) {
            return new Result(ResultCode.SUCCESS);
        } else {
            return new Result(ResultCode.FAIL);
        }
    }
}
